using System.Collections.Generic;

namespace Catastrophe.Common.ObjM
{
	public sealed class SafeDictionary<TKey, TValue>
	{
		private readonly object _Padlock = new object();

		private readonly Dictionary<TKey, TValue> _Dictionary;

		public TValue this[TKey key]
		{
			get
			{
				lock (this._Padlock)
				{
					return this._Dictionary[key];
				}
			}
			set
			{
				lock (this._Padlock)
				{
					this._Dictionary[key] = value;
				}
			}
		}

		public SafeDictionary()
		{
			this._Dictionary = new Dictionary<TKey, TValue>();
		}

		public bool TryGetValue(TKey key, out TValue value)
		{
			lock (this._Padlock)
			{
				return this._Dictionary.TryGetValue(key, out value);
			}
		}

		public int Count()
		{
			lock (this._Padlock)
			{
				return this._Dictionary.Count;
			}
		}

		public void Add(TKey key, TValue value)
		{
			lock (this._Padlock)
			{
				if (!this._Dictionary.ContainsKey(key))
				{
					this._Dictionary.Add(key, value);
				}
			}
		}
	}
}
